package models

import (
	"awesome-devops/boot"
	"awesome-devops/helper"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	"sync"
)

var locker sync.Mutex

const NEED_UPLOAD = 1

type Process struct {
	BaseModel
	Name                 string            `json:"name"`
	ID                   uint              `json:"id"`
	ServerIdJson         string            `json:"server_id_json"`
	ProjectId            uint              `json:"project_id"`
	BeforeUploadStepJson string            `json:"before_upload_step_json"`
	AfterUploadStepJson  string            `json:"after_upload_step_json"`
	NeedUpload           int               `json:"need_upload"`
	OutputDir            string            `json:"output_dir"`
	Files                string            `json:"files"`
	UploadDir            string            `json:"upload_dir"`
	UploadExcludeDir     string            `json:"upload_exclude_dir"`
	ExtParams            string            `json:"ext_params"`
	CreateTime           string            `json:"create_time"`
	UpdateTime           string            `json:"update_time"`
	DeleteTime           int               `json:"delete_time"`
	RunTimeEnv           map[string]string `gorm:"-"`
}

func (process *Process) GetServers() []ServerConfig {
	var serverJsonList []int
	err := commonhelper.JsonDecodeWithType(process.ServerIdJson, &serverJsonList)
	if err != nil {
		return make([]ServerConfig, 0)
	}
	return FindServerByIds(serverJsonList)
}

func (process *Process) SetEnv(key string, value string) {
	locker.Lock()
	if process.RunTimeEnv == nil {
		process.RunTimeEnv = map[string]string{}
	}
	process.RunTimeEnv[key] = value
	locker.Unlock()
}
func (process *Process) GetExtParams(key string, defaultValue string) string {
	params := helper.GetCommandParamsFormStr(process.ExtParams)
	if value, ok := params[key]; ok {
		return value
	}
	return defaultValue
}

func (process *Process) MergeEnv(args map[string]string) {
	locker.Lock()
	if process.RunTimeEnv == nil {
		process.RunTimeEnv = map[string]string{}
	}
	for key, value := range args {
		process.RunTimeEnv[key] = value
	}
	locker.Unlock()
}

func (process *Process) GetRunningEnv() map[string]string {
	if process.RunTimeEnv == nil {
		return map[string]string{}
	}
	return process.RunTimeEnv
}

func (process *Process) GetRunStep() []RunStep {
	var serverJsonList []int
	err := commonhelper.JsonDecodeWithType(process.BeforeUploadStepJson, &serverJsonList)
	if err != nil {
		return make([]RunStep, 0)
	}
	var stepRunData []RunStep
	step := RunStep{}
	runStep := RunStep{}
	stepRunDataInterface, err := runStep.RowsByIDs(stepRunData, step.TableName(), serverJsonList)
	if err != nil {
		return make([]RunStep, 0)
	}
	return stepRunDataInterface.([]RunStep)
}

func (process *Process) GetRunFiles() []ProcessFiles {
	var serverJsonList []int
	err := commonhelper.JsonDecodeWithType(process.Files, &serverJsonList)
	if err != nil {
		return make([]ProcessFiles, 0)
	}
	var stepRunData []ProcessFiles
	step := ProcessFiles{}
	runStep := RunStep{}
	stepRunDataInterface, err := runStep.RowsByIDs(stepRunData, step.TableName(), serverJsonList)
	if err != nil {
		return make([]ProcessFiles, 0)
	}
	return stepRunDataInterface.([]ProcessFiles)
}

func (process *Process) GetAfterRunStep() []RunStep {
	var serverJsonList []int
	err := commonhelper.JsonDecodeWithType(process.AfterUploadStepJson, &serverJsonList)
	if err != nil {
		return make([]RunStep, 0)
	}
	var stepRunData []RunStep
	step := RunStep{}
	runStep := RunStep{}
	stepRunDataInterface, err := runStep.RowsByIDs(stepRunData, step.TableName(), serverJsonList)
	if err != nil {
		return make([]RunStep, 0)
	}
	return stepRunDataInterface.([]RunStep)
}

func (process *Process) TableName() string {
	return "ops_publish_process"
}

func (process *Process) GetProject() *Project {
	project := Project{}
	err := project.RowByID(int(process.ProjectId), &project)
	if err != nil {
		return nil
	}
	return &project
}

func (process *Process) GetFiles() []ProcessFiles {
	processFiles := ProcessFiles{}
	var processFilesList []ProcessFiles
	boot.GetDbConnection().Table(processFiles.TableName()).
		Where("process_id", process.ID).
		Where("delete_time=0").
		Find(&processFilesList)
	return processFilesList
}

func FindProcessByProjectId(projectId int) []Process {
	var processList []Process
	process := Process{}
	boot.GetDbConnection().Table(process.TableName()).
		Where("project_id", projectId).
		Where("delete_time=0").
		Find(&processList)
	return processList
}
